﻿//
// GNS_TopAndTick.cpp
// MobileManageSys
//FuncTion  充值兑奖
// Created by mr.l on 16/1/15.
//
//
#include "GnsClientFrame.h"
#include "GNS_TopAndTick.h"
#include "cocostudio/CocoStudio.h"
#include "ui/CocosGUI.h"
#include "GNS_PlayerMgr.h"
#include "GNS_Common.h"
#include <algorithm>
#include "GNS_SceneMgr.h"
#include "GnsOften.h"

USING_NS_CC;
using namespace cocostudio::timeline;
using namespace ui;

#define winsize Director::getInstance()->getVisibleSize()
//extern ISocketMgr * _GameSock;
GNS_TopAndTick::GNS_TopAndTick()
{
    pGNS_TopAndTick=nullptr;
    pTopAndTickDivider=nullptr;
    //pSearch=nullptr;
    pListView=nullptr;
//    InPutText=nullptr;
    pPopWnd=nullptr;
	inputSearchText = nullptr;
	_inputSearchText = nullptr;

}

GNS_TopAndTick::~GNS_TopAndTick()
{
    
}

GNS_TopAndTick* GNS_TopAndTick::createLayer()
{
    auto pGNS_TopAndTick = GNS_TopAndTick::create();
    auto pScene = Director::getInstance()->getRunningScene();
    if (!pScene)
        return 0;
    pScene->addChild(pGNS_TopAndTick);
    return pGNS_TopAndTick;
}

bool GNS_TopAndTick::init()
{
    if ( !Layer::init() )
    {
        return false;
    }
    return true;
}
void GNS_TopAndTick::SetNodeName(std::string CsbName)
{
    pGNS_TopAndTick = CSLoader::createNode(CsbName);
    pGNS_TopAndTick->setContentSize(winsize);
    ui::Helper::doLayout(pGNS_TopAndTick);
    addChild(pGNS_TopAndTick,11);
    
    auto pTitText = static_cast<Text*>(pGNS_TopAndTick->getChildByName("Text_5"));
    auto pImageView = static_cast<ImageView*>(pGNS_TopAndTick->getChildByName("Image_9"));
    auto pTitText1= static_cast<Text*>(pImageView->getChildByName("Text_7"));
	inputSearchText = static_cast<TextField*>(pGNS_TopAndTick->getChildByName("searchTextField"));
	
	//请输入推广员账号
   
    /*pSearch= GNS_CursorTextField::textFieldWithPlaceHolder("请输入推广员账号",GameFontName, 24);
    pSearch->setColor(Color3B::BLACK);
    pSearch->setAnchorPoint(Vec2(0, 0.5f));
    pSearch->setPosition(Vec2(winsize.width*0.28, winsize.height*0.8621));
    pSearch->setHorizontalAlignment(TextHAlignment::LEFT);
    
   // pSearch->AddCursor(pGNS_TopAndTick);
    pSearch->setLimitNum(10);
    */
	std::string PlaceHolder = "";
	int nAgentLevent = GNS_PlayerMgr::getInstance()->getUserLevel();
    if (nAgentLevent==0) {
        pTitText->setString(CCGnsOften::getUtf8_DirectAssignmentInProgram("本运营与代理充/兑记录"));
        pTitText1->setString(CCGnsOften::getUtf8_DirectAssignmentInProgram("代理商账号"));
		PlaceHolder = "请输入代理商账号";
    }
    else if (nAgentLevent == 1){
		PlaceHolder = "请输入推广员账号";
		
    }
	inputSearchText->setPlaceHolder(CCGnsOften::getUtf8_DirectAssignmentInProgram(PlaceHolder));
	inputSearchText->setColor(Color3B::BLACK);
    GNS_PlayerMgr::getInstance()->_NodeChildVec.pushBack(pGNS_TopAndTick);
	GNS_NodeMgr::getInstance()->pushChildNode("GNS_TopAndTick", pGNS_TopAndTick);

	_inputSearchText = GNS_Handler::createTMEditBoxWithExistFeild(inputSearchText, pGNS_TopAndTick, 10, EditBox::InputMode::NUMERIC, true);
	inputSearchText->setVisible(false);
	_inputSearchText->setColor(Color3B::BLACK);

    auto pBackBtn = static_cast<Button*>(pGNS_TopAndTick->getChildByName("TopAndTickBtn"));
    pBackBtn->setTag(1);
    pBackBtn->addTouchEventListener(CC_CALLBACK_2(GNS_TopAndTick::BtnCallBack, this));
    
    auto pChoosebutton = static_cast<Button *>(pGNS_TopAndTick->getChildByName("Button_12"));
    pChoosebutton->setTag(3);
    pChoosebutton->addTouchEventListener(CC_CALLBACK_2(GNS_TopAndTick::BtnCallBack, this));
    
    pListView = static_cast<ListView *>(pGNS_TopAndTick->getChildByName("ListView_1"));
    
    auto pHelp = static_cast<Button*>(pGNS_TopAndTick->getChildByName("Button_5"));
    pHelp->setTag(4);
    pHelp->addTouchEventListener(CC_CALLBACK_2(GNS_TopAndTick::BtnCallBack,this));
    
    auto pHelp1 = static_cast<Button*>(pGNS_TopAndTick->getChildByName("Button_6"));
    pHelp1->setTag(5);
    pHelp1->addTouchEventListener(CC_CALLBACK_2(GNS_TopAndTick::BtnCallBack,this));
    
}
void GNS_TopAndTick::BtnCallBack(cocos2d::Ref *pSender, ui::Widget::TouchEventType type){
    if (type==ui::Widget::TouchEventType::ENDED) {
        auto pButton = (Button*)pSender;
        int BtnTag = pButton->getTag();
        switch (BtnTag) {
            case 1:
            {
                if (GNS_PlayerMgr::getInstance()->_NodeChildVec.size()!=0) {
                    GNS_PlayerMgr::getInstance()->_NodeChildVec.clear();
                    this->removeAllChildrenWithCleanup(true);
                    this->getParent()->removeChild(this);    
                }
				//GNS_NodeMgr::getInstance()->eraseChildNode("GNS_TopAndTick");

                //auto PromoterNode=GNS_PlayerMgr::getInstance()->_NodeHallVec.at(1);
                //PromoterNode->setVisible(true);

				GNS_NodeMgr::getInstance()->setMenuVisible("Promoter", true);
                break;
            }
            case 2:
            {
                pGNS_TopAndTick->setVisible(false);
                pTopAndTickDivider = GNS_TopAndTickDivider::createLayer();
                pTopAndTickDivider->SetNodeName("TopAndTickDetails.csb");
                
                break;
            }
            case 3:
            {
				//std::string pTextStr = inputSearchText->getString();  //pSearch
				std::string pTextStr = _inputSearchText->getText();
                log("%s",pTextStr.c_str());
                if (GNS_PlayerMgr::getInstance()->getExListRes()->size()!=0) {
                    pListView->removeAllItems();
                    bool IsSearch =false;
                    int x=0;
                    for (auto it:*(GNS_PlayerMgr::getInstance()->getExListRes())) {
                        if (it._lowlineCode==atoi(pTextStr.c_str())) {
                            IsSearch=true;
                            auto pDateButton = Button::create("line between1.png","line between2.png");
                            pDateButton->setTag(100+x);
                            pDateButton->addTouchEventListener(CC_CALLBACK_2(GNS_TopAndTick::ListViewBtn, this));
                            pListView->addChild(pDateButton);
                            
                            auto pIDText = Text::create(StringUtils::format("%d",it._lowlineCode),GameFontName ,25);
                            pIDText->setColor(Color3B::BLACK);
                            pIDText->setPosition(Vec2(pDateButton->getContentSize().width*0.0784, pDateButton->getContentSize().height*0.4));
                            pDateButton->addChild(pIDText);
                            
                            auto pGoldText =Text::create(StringUtils::format("%lld",it._lowlineGold), GameFontName, 25);
                            pGoldText->setColor(Color3B::BLACK);
                            pGoldText->setPosition(Vec2(pDateButton->getContentSize().width*0.41, pDateButton->getContentSize().height*0.4));
                            pDateButton->addChild(pGoldText);
                            
                            auto pTopText = Text::create(StringUtils::format("%lld",it._lowlineRecharge), GameFontName, 25);
                            pTopText->setColor(Color3B::BLACK);
                            pTopText->setPosition(Vec2(pDateButton->getContentSize().width*0.64, pDateButton->getContentSize().height*0.4));
                            pDateButton->addChild(pTopText);
                            
                            auto pTickText = Text::create(StringUtils::format("%lld",it._lowlineReward), GameFontName, 25);
                            pTickText->setColor(Color3B::BLACK);
                            pTickText->setPosition(Vec2(pDateButton->getContentSize().width*0.89, pDateButton->getContentSize().height*0.4));
                            pDateButton->addChild(pTickText);
                            
                            break;
                        }
                        x++;
                    }
                    if (IsSearch==false) {
                        auto pPopWnd = PopWndLayer::createScene("Popwnd.png", "PageClose.png");
                        pPopWnd->setClickWndOutsideClose(true);
                        pPopWnd->SetTitleWithUTF("提示!");
                        pPopWnd->SetContentTextWithUTF("没有搜索到", Color3B::BLACK);
                        ShowTopTickListDate();
                    }
                }else{
                    auto pPopWnd = PopWndLayer::createScene("Popwnd.png", "PageClose.png");
                    pPopWnd->setClickWndOutsideClose(true);
                    pPopWnd->SetTitleWithUTF("提示!");
                    pPopWnd->SetContentTextWithUTF("没有搜索到", Color3B::BLACK);
                    ShowTopTickListDate();
                }
               
                break;
            }
            case 4:  //问号 1，
            {
                pPopWnd = PopWndLayer::createScene("Calendarbg.png","understand.png");
                auto node = pPopWnd->Loadcsb("help1Layer.csb");
				node->getChildByName("Text_tuidai_dj")->setVisible(false);
                node->getChildByName("Text_daitui_cz")->setVisible(true);

                pPopWnd->GetCloseButton()->setPosition(Vec2(pPopWnd->GetPopWndSize()->getContentSize().width/2, pPopWnd->GetPopWndSize()->getContentSize().height*0.1));
                pPopWnd->GetCloseButton()->setScale(0.7f);
                
                break;
            }
            case 5:
            {
                pPopWnd = PopWndLayer::createScene("Calendarbg.png","understand.png");
				auto node = pPopWnd->Loadcsb("help1Layer.csb");
				node->getChildByName("Text_tuidai_dj")->setVisible(true);
				node->getChildByName("Text_daitui_cz")->setVisible(false);

                pPopWnd->GetCloseButton()->setPosition(Vec2(pPopWnd->GetPopWndSize()->getContentSize().width/2, pPopWnd->GetPopWndSize()->getContentSize().height*0.1));
                pPopWnd->GetCloseButton()->setScale(0.7f);
                break;
            }
            default:
                break;
        }
    }
}
void GNS_TopAndTick::ShowTopTickListDate()
{
    pListView->removeAllItems();
    int x=0;
    Size bgsize;
    int index =(int)GNS_PlayerMgr::getInstance()->getExListRes()->size();
    for (auto it:*(GNS_PlayerMgr::getInstance()->getExListRes())) {
        
        auto pDateButton = Button::create("line between1.png","line between2.png");
        pDateButton->setTag(100+x);
        pDateButton->addTouchEventListener(CC_CALLBACK_2(GNS_TopAndTick::ListViewBtn, this));
        pListView->addChild(pDateButton);
        bgsize=pDateButton->getContentSize();
        
        auto pIDText = Text::create(StringUtils::format("%d",it._lowlineCode),GameFontName ,25);
        pIDText->setColor(Color3B::BLACK);
        pIDText->setPosition(Vec2(pDateButton->getContentSize().width*0.0784, pDateButton->getContentSize().height*0.4));
        pDateButton->addChild(pIDText);
        
        auto pGoldText =Text::create(StringUtils::format("%lld",it._lowlineGold), GameFontName, 25);
        pGoldText->setColor(Color3B::BLACK);
        pGoldText->setPosition(Vec2(pDateButton->getContentSize().width*0.41, pDateButton->getContentSize().height*0.4));
        pDateButton->addChild(pGoldText);
        
        auto pTopText = Text::create(StringUtils::format("%lld",it._lowlineRecharge), GameFontName, 25);
        pTopText->setColor(Color3B::BLACK);
        pTopText->setPosition(Vec2(pDateButton->getContentSize().width*0.64, pDateButton->getContentSize().height*0.4));
        pDateButton->addChild(pTopText);
        
        auto pTickText = Text::create(StringUtils::format("%lld",it._lowlineReward), GameFontName, 25);
        pTickText->setColor(Color3B::BLACK);
        pTickText->setPosition(Vec2(pDateButton->getContentSize().width*0.89, pDateButton->getContentSize().height*0.4));
        pDateButton->addChild(pTickText);
        x++;
    }
    pListView->setInnerContainerSize(Size(bgsize.width, (bgsize.height+10)*index));
    
}
void GNS_TopAndTick::SelectedItemEvents(cocos2d::Ref *pSender, ui::ListView::EventType type)
{
    if (type==ui::ListView::EventType::ON_SELECTED_ITEM_START) {
        auto pListView = static_cast<ListView*>(pSender);
        CC_UNUSED_PARAM(pListView);
        CMD_SC_GetExchangeListResult pExListRes;
        int index=(int)pListView->getCurSelectedIndex();
        int x=0;
        for (auto it:*(GNS_PlayerMgr::getInstance()->getExListRes())) {
            if (x==index) {
                pExListRes=it;
                break;
            }
            x++;
        }
        pGNS_TopAndTick->setVisible(false);
        pTopAndTickDivider = GNS_TopAndTickDivider::createLayer();
        pTopAndTickDivider->SetNodeName("TopAndTickDetails.csb");
        
        CMD_CS_GetSingleAgentInfo pSingleAgInfo;
        pSingleAgInfo._uplineCode=(int)GNS_PlayerMgr::getInstance()->getMyUseridx();
        pSingleAgInfo._agentCode=pExListRes._lowlineCode;
        //_GameSock->sendData(MAIN_MSG_LOGIN, SUB_CS_GET_SINGLE_AGENT_INFO, &pSingleAgInfo, sizeof(pSingleAgInfo));
        GnsClientFrame::getInstance()->sendMsg(0,MAIN_MSG_LOGIN, SUB_CS_GET_SINGLE_AGENT_INFO, (char*)&pSingleAgInfo, sizeof(pSingleAgInfo));
    }
}
void GNS_TopAndTick::ListViewBtn(cocos2d::Ref *pSender, ui::Widget::TouchEventType type)
{
    if (type==ui::Widget::TouchEventType::ENDED) {
        auto pButton = (Button*)pSender;
        int Btntag = pButton->getTag();
        int index =0;
        CMD_SC_GetExchangeListResult pExListRes;
        for (auto it:*(GNS_PlayerMgr::getInstance()->getExListRes())) {
            if (Btntag-100==index) {
                pExListRes=it;
                break;
            }
            index++;
        }
        pGNS_TopAndTick->setVisible(false);
        pTopAndTickDivider = GNS_TopAndTickDivider::createLayer();  //this area 
       //pSearch = nullptr;
        pTopAndTickDivider->SetNodeName("TopAndTickDetails.csb");
        
        CMD_CS_GetSingleAgentInfo pSingleAgInfo;
        pSingleAgInfo._uplineCode=(int)GNS_PlayerMgr::getInstance()->getMyUseridx();
        pSingleAgInfo._agentCode=pExListRes._lowlineCode;
        //_GameSock->sendData(MAIN_MSG_LOGIN, SUB_CS_GET_SINGLE_AGENT_INFO, &pSingleAgInfo, sizeof(pSingleAgInfo));
       GnsClientFrame::getInstance()->sendMsg(0,MAIN_MSG_LOGIN, SUB_CS_GET_SINGLE_AGENT_INFO, (char*)&pSingleAgInfo, sizeof(pSingleAgInfo));
    }
}